package cn.tedu.mall.seckill;

import cn.tedu.mall.seckill.init.SeckillInitializer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author zhangyiping
 * @date 2024/1/19
 */
@SpringBootApplication
@MapperScan(basePackages = {
        "cn.tedu.mall.seckill.mappers"
})
@ComponentScan(basePackages = {
        "cn.tedu.mall.seckill",
        "cn.tedu.mall.common.exception.handler",
        "cn.tedu.mall.common.utils"
})
@EnableDubbo
public class SeckillApplication {
    public static void main(String[] args) {
        /* run 方法调用,spring容器,springboot启动流程开始加载了,我们要在run之前,
        添加一个异步的监听回调.监听器作用就是从程序开始一直到启动结束,
        观察、监控程序的状态(条件事件event),满足监听要求的时候,才开始运行监听方法
         */
        SpringApplication springApplication = new SpringApplication(SeckillApplication.class);
        // 执行监听事件
        springApplication.addListeners(new ApplicationListener<ApplicationReadyEvent>() {
            @Override
            public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
                // ApplicationReadyEvent事件后，spring应用已经启动完成，此时可以使用容器中所有的bean对象
                ConfigurableApplicationContext applicationContext = applicationReadyEvent.getApplicationContext();
                SeckillInitializer initializer = applicationContext.getBean(SeckillInitializer.class);
                initializer.initStart();
                // 启动定时任务，将数据库中sku库存数同步到redis
                // initializer.syncStock();
            }
        });
        springApplication.run(args);
    }
}
